package com.maoye.ossp.biz.brand.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.maoye.ossp.biz.brand.entity.Supplier;
import com.maoye.ossp.biz.brand.entity.SupplierBank;
import com.maoye.ossp.biz.brand.entity.SupplierRegister;
import com.maoye.ossp.biz.brand.service.ISupplierRegisterService;
import com.maoye.ossp.biz.brand.service.ISupplierService;
import com.maoye.ossp.common.web.BaseController;

@Controller
@RequestMapping("/biz/brand/supplierRegister/")
public class SupplierRegisterController extends BaseController {
	@Resource
	private ISupplierRegisterService supplierRegisterService;
	@Resource
	private ISupplierService supplierService;

	/**
	 * @param supplier添加或更新的实体
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value = "save")
	public String save(SupplierRegister supplierRegister, Model model,
			RedirectAttributes redirectAttributes,HttpServletRequest request) 
	{
		String msg = null;
		try 
		{
			supplierRegisterService.saveDate(supplierRegister);
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
			msg = "操作失败";
			addMessage(model, msg);
		}
		String navId = request.getParameter("navId");
		model.addAttribute("navId", navId);
		setSupplierInfo(supplierRegister, model);
		return "/biz/brand/supplier/apply";
	}
	
	@RequestMapping(value = "add")
	public String add(SupplierRegister supplierRegister, Model model,HttpServletRequest request) throws Exception
	{
		String curSupplierId = request.getParameter("curSupplierId");
		if(null == curSupplierId || "".equals(curSupplierId))
		{
			return _500;
		}
		supplierRegister = supplierRegisterService.getBySupplierAccountId(Long.valueOf(curSupplierId));
		model.addAttribute("supplierRegister", supplierRegister);
		return "/biz/brand/supplierRegister/add";
	}

	/**
	 * 编辑供应商银行信息
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "{id}/edit")
	public String edit(@PathVariable String id, Model model,
			RedirectAttributes redirectAttributes) 
	{
		SupplierRegister supplierRegister = supplierRegisterService.get(Long.valueOf(id));
		model.addAttribute("supplierRegister", supplierRegister);
		return "/biz/brand/supplierRegister/add";
	}
	
	private void setSupplierInfo(SupplierRegister supplierRegister, Model model)
	{
		Supplier supplier = supplierService.get(supplierRegister.getSupplierAccountId());
		if(supplier == null)
		{
			supplier = new Supplier();	
		}
		SupplierBank supplierBank = new SupplierBank();
		supplierBank.setSupplierBankList(supplier.getSupplierBankList());
		supplierBank.setSupplierId(supplier.getId());
		model.addAttribute("supplier",supplier);
		model.addAttribute("supplierBank",supplierBank);
		model.addAttribute("tabId", "bank");
	}
}
